{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "b805a5ae",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:17:13.886467Z",
     "start_time": "2023-10-14T14:17:13.879179Z"
    }
   },
   "outputs": [],
   "source": [
    "from IPython.display import Image\n",
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10e505df",
   "metadata": {},
   "source": [
    "## basics"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "deb2ab43",
   "metadata": {},
   "source": [
    "- `tensor.view`/`torch.reshape`\n",
    "    - `torch.transpose()`\n",
    "- `torch.cat`\n",
    "- `torch.stack`\n",
    "- `torch.flip`/`torch.swapaxes`\n",
    "- `torch.flatten`\n",
    "- `torch.chunk`\n",
    "- `torch.expand`/`repeat`: broadcast??\n",
    "- `torch.squeeze()`/`torch.unsqueeze()`\n",
    "- `torch.transpose()`/`torch.permute()`\n",
    "- reverse: 注意不是 `[::-1]`（torch 中不支持负数的 step）\n",
    "    - torch.flip"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d42592c2",
   "metadata": {},
   "source": [
    "## `torch.view` vs. `torch.reshape`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54d16482",
   "metadata": {},
   "source": [
    "- 都可以重新组织 shape，\n",
    "- `Tensor.view` 返回的是视图，数据共享，有 contiguous 约束要求\n",
    "- Contiguous\n",
    "    - https://stackoverflow.com/questions/26998223/what-is-the-difference-between-contiguous-and-non-contiguous-arrays\n",
    "    - https://stackoverflow.com/questions/49643225/whats-the-difference-between-reshape-and-view-in-pytorch\n",
    "- 从 robust（接口适配面上）`Tensor.reshape()` is more robust，没有 contiguous 约束要求 \n",
    "    - `Tensor.reshape()`, It will work on any tensor, \n",
    "    - `Tensor.view()` works only on tensor `t` where `t.is_contiguous()==True`.\n",
    "        - `t.contiguous().view()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "21348b80",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:44:14.283682Z",
     "start_time": "2023-10-07T12:44:14.271621Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1.],\n",
       "        [1., 1.],\n",
       "        [1., 1.]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = torch.zeros(3, 2)\n",
    "x = z.view(2, 3)\n",
    "print(x.is_contiguous())\n",
    "# z.fill_(1)\n",
    "# x\n",
    "x.fill_(1)\n",
    "z"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "bfccc677",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:44:43.241687Z",
     "start_time": "2023-10-07T12:44:43.227292Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1.],\n",
       "        [1., 1.],\n",
       "        [1., 1.]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# reshape \n",
    "z = torch.zeros(3, 2)\n",
    "x = z.reshape(2, 3)\n",
    "print(x.is_contiguous())\n",
    "\n",
    "# z.fill_(1)\n",
    "# x\n",
    "\n",
    "x.fill_(1)\n",
    "z"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b6cdef7",
   "metadata": {},
   "source": [
    "### 底层数据存储"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "a7912c1b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:45:02.068477Z",
     "start_time": "2023-10-07T12:45:02.058152Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1, 2, 3],\n",
       "        [4, 5, 6, 7]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.arange(8).reshape(2, 4)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "fee0bf3e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T11:56:08.256056Z",
     "start_time": "2023-10-07T11:56:08.248951Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAKUAAABOCAYAAABBqNabAAAACXBIWXMAAB6aAAAemgH8byj4AAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAENBJREFUeJztnXlwW9d1h7/7ABAgCC7gIokUJZFaLUuWvCZym8p2mjSJW6tqHHuciadjT+LUnWmmf9SJO9NM27TN1E2nTpvWjZsumTTeYqfOKLHHcWI73hLFduJFlmUpXLRwE8UNBECC2N7tHwcQSHCRTPAtsd83g3lvgEe8w4vz7nLuvecHlbML+KZSDAA5QNvyUsygOAx8CWgss6kKuB3FiyjittkEWilGgQPAtQuU1TbgXhSngKzNZXUUuAtYXWaTD7gFxU+UYtLmshoHngCuB1TRoLMny+RO4O+VH123ESMYBSNQ4TeeJ7kUpIbQU4MoFGNo9gMvAutQPIZmV7ABM9KBEaih8v/0PNB5yCQg3o2Zn8EAHgJuBWaQh+RflcJX24kKNdtXVvkZmD6NnhoAIIHmRuBJYBWKA2j2VNVh1nZi+GtAGdbbpE3IJiDeg5mbxgAeB24CkpX8VH8M/Hv9FvSGfahALaAhOyVHu/655CnofYR8NkkKzVUo7lc+tm74PYym3djijOWYWRj8CQz/DIBvI7XB/ZH10Hk9qqrefpsApoeg92HMdIwsmmtQfE0pLlv3UVTz5aXfy8xBbkrOAxFQPuts0nk4/QIMPodG8ziwb7k/WQuK4zVrCW+7VX720y/CyCuQTcoF/mpovgxarwbDvyL2L8rMGLx9L6aZ5TiwaeONEL2w9Hk2DlODkBoWh/FXw+rftNYmgL4n4MxLoBTxUAuRC27DMAJgZmCyC+I9kI5Bfhp8IahZB82XQjBqn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P3naNczHfhm0Stw3UOCc3Y5lTVrc4LxJat0lebiUYlSlFNxFqlpeTvKv7lB6/nnhO6eE6PKf0cB2eU3q4Ds8pPVyH55QersNzSg/X4Tmlh+vwnNLDdXhO6eE6PKf0cB3vGsmSs/a4zS6FO21yI1JOy5csySbdJVlimnLMJJy1oxydh9yMuxINFG3Jxp21o5z0JCijAsmS/AxGzMZdgOdi8pisPxx9zT2PSi4FmZi0KsXUJ25g/E1ZAT/6qntaPDML44cwtVmBZIlSJLvuQ5suEC2aGoC+H2JqTdfkMdTwQactEnoeADOLUgaT3Q9gLrafyU5mRuHkAUwNJ6cGUMWtD05z/FHIJlZAsiQ9znXxbnTjTudkOCaOwBtfIZ9PiWSJUuwd+QVNwSgq0uGMDEc+Dd33yz4c4Fto7s7GuSF2BKI7UQGHNtHFu+H1uzAzkyXJkrHXaAvUoOo2OSRZkoXe78Lx/wNWUrLE8EN0J0a41d6c5/FezEQvhjIYKeQ8nyNZEmqRnOdVDTbmPB8r5DyfXkSyxMAf3Q417fZpD+WmIXECPdmFUgaxQs7zOZIlwUbMxh2FsrIr5/l4Ied5wiLJEmXQh7JXskQZ55AsUbygDBdKlhgOSJYY5yFZYrhDsuT/AU8N8/18d0FAAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image('./imgs/shape_2x4.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b6dfc311",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T11:56:38.887276Z",
     "start_time": "2023-10-07T11:56:38.878786Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAT8AAAAnCAYAAAB5VE/pAAAACXBIWXMAAB6aAAAemgH8byj4AAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAEXVJREFUeJ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LrQw9LahdB8HVgHVKBYi+YCXzFmaQMeXzGOBYZjARjZgxUZwEDxRzSXAweAc1D8Fk1FUQ26cL5zDYY2ZXXmoRYsK4YCfgR8P1H8z8Ddhg9duhgjr9y5F9qMQLAdK9CGgaIPzVXAG2lVbkLxoFL4KpqhqAblcWiHOzMKgTZ0/1a0toijuRX4L6AQeBS42leCOeskPHkVzu21EhuB/m1YwXYMZbBdW1wG7AROUQbPaovqkjqsskYMx/alsWSJ/N6PMONBPMDPgduAZL/QC/wDijvReH3FmJ4Cx4ZqdGwYZYYxlMGQtrgTeBimxyQ0Ad9BsQpNNdO4OvSEKKJAK5pngXuBtlGlcB3wDWVwirYYZwdTG2QphrTmQ+DXSMuXHhioBO5Qiqs0LEaT75AsSym6tOYV4D7g3THlpwN/pQzO1xZzcSoFShFWsFNrfpvQ1ZehVj1wpzK4RGtq0Dhj/hQxpTigLV4AfgzsHlPjcuBmZbBSWzi2s4kyGNGaLWgeR4xMJK24BPi2UlwDNGlNgUOyLGXQoy3eAB4AXh+nXh1wLdAMjmkDGAI2AU+R9oz9P1OY9qVIYofUAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 内存结构，memory allocation\n",
    "# row-wise\n",
    "Image('./imgs/shape_1x8.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "c3203459",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:45:30.113950Z",
     "start_time": "2023-10-07T12:45:30.108678Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 1)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# stride() gives the number of bytes required to go to the next element in each dimension:\n",
    "# 4：从第0行跳转到第1行，所需的字节数\n",
    "# 从 0 跳转大 4\n",
    "a.stride()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b002d109",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T11:45:49.865366Z",
     "start_time": "2023-10-07T11:45:49.856484Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1],\n",
       "        [2, 3],\n",
       "        [4, 5],\n",
       "        [6, 7]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.view(4,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "60f11436",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T11:57:21.899022Z",
     "start_time": "2023-10-07T11:57:21.890457Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFwAAACdCAYAAAA5Wx9JAAAACXBIWXMAAB6aAAAemgH8byj4AAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAEw9JREFUeJztnXl0VFWexz/3VfaFLAQCIUiAhIjILor7uLCoreA4aovdjdrd2ra2Ou24dzujbTsuo7bT47RLK32cdm23dsHddgXhIKAgEAwkkASSkBCSkKQqqffu/PGrIoEsQL2qWyWnPucU752ql7q/+vLeXX+/+1O4pwD4NYpzgMPQJIfhOw8EWynqtOZ94EHgm36umwScB4wFkgzZBtAEfAa8ArQH31Quv/QcFM8CaRkjISUP5fobD4LOZmitwNE2CrgVuKfHxxnAo8ACQCWkYlvJZqzTDtrfhtI2llLUas1PgcXgTvATUXyYkotnzPlYqcPkTdsHjg8SMkBZ7o3fH10tUPk6uqUcBVwNPAKkoPgYzTFDjoZhx0FSduRt6Ym2ofk7qH4Xx9cEwPnAK6EK7kGxJjGN0vFXYiVmwM41sP1T8O6QC6xEyJkAhbMgIT0sv6FftA1li9BtNXSgGQNcA9w66mzIm959Xecu8Ace7tR8UJ7I2gVgd8CGp3C8DbShGRtqkScAN46YhRo0Gmo/h61vgb8D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rYcPj0vUqOlfqy2jTvBFqlwAKxi2Mjg1hqVlnPiBZB1ff072bG8jc8qTr4ei7MbMf5n7Y8CSgoeCU6O3rEhbBlQXTfit5kOuWSKRvco4M92Mpkm3ab2DKzXvv9maasPYdEjOi0xAdKAcygxhp4s6chokLbpi44IaJC26YuOCGiQtumLjghokLbpi44IaJC26YuOCGCVVwDf3760WLgD09/40ZAgvuOlTBG0F8O2IGDR11ANRDjNmG2KY8NIUq+HKIrbDBuqXQuQsL+EBZ7K5aHG2LuvF3QM0HONpmSaiCL1EW61bdhWPCM3Z/OF2w9Hq0smgD/qodFlW+DjUfRNsyYeUd4GvCAh4PVXBHO1zu3YH9xsk4Qf/CaNBWDe/8AF3/JUo73IBUKXcoxdZ35+Nsep6o1ef+Dlh+M6y+D4CXgTfcLnydrSyeRZM+dCbkHIEyuTDbWgnb/oHj+AHNLcB9PT4uURZvaodxg8biDDsBy+SG8r4mqUYCd/bLwEIkD6lrhgP3K4sypfBiKkpM4VceqoEngQn92JYCXIviS2Wx25htoJWHBiTb4Nk9Dfp/Tw4CR+gIryIAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image('./imgs/shape_4x2.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "25e5d8aa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:04:08.789716Z",
     "start_time": "2023-10-07T12:04:08.778558Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAT8AAAAnCAYAAAB5VE/pAAAACXBIWXMAAB6aAAAemgH8byj4AAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAEdJJREFUeJztnXl0VdW9xz/7nAw3IQmZgTAGwqQEVARErTylVByrqNShTlhXX99TQap9z/Y9O71ah6WWqiwttVbrs12K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n8tvcjQaSpfBxzdgDi9FAfcDb3XWNMwGHlc2KmMoVnIOWF22PWr76Cbwl2JqZOjtx3AL8DqQheI1DFMT03AyhmEnpuFaYLipDmp3oxuOYaFYg+FK4AAwRVm8YTRZGUWYjGGoBJfe1qaD0klUrEM7DSjgUeCBUPHDwP12MiZnHJYvz717GAxImKJ6B5ayqDSaq4GlzarcqhR/wCIxfyJkDEW59YY7JwDVuzHlKzHaIYjhR8CfgFTgL8CMpEycvudhp+S5ZwQbjsHRr9A1e7CUxVajuQLYDoxXNm8bh345Z6D7nIPl1i7qOihb5B/4AKexGht4DrgLCI8LE4DfoJiLISE5Gycpw6UWaTCBClRTLZayqDaaucDz0DUmYTRwL4rpGPrRhbtDt4uiEdiL4W3gCeBgi1KYCdyhLMYbTbYrmgClqDaGtcDLSM/XPDCQDcxRFlcbQxEGt15WqZVFqdF8AswDVrUqnwjcoywuMpo+uJUCpQgoxXajeSOkqzJKrUJgrrK4zBj6Y3DHP1U0KcUBo3kfeBzY1arGlcAPlM1k4+CaL6Msao1hI4a/IkameQQyHbhbWVwDjDYat94JqJVNuXFYCjwDLGmj3hDgOmAMuKYNoBpYCSyk2TP2v+LhCdLFxcGqAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image('./imgs/shape_1x8_2.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b37c06d7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T11:46:01.951209Z",
     "start_time": "2023-10-07T11:46:01.943610Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2, 1)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.view(4, 2).stride()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "bb27daa3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T11:59:02.597469Z",
     "start_time": "2023-10-07T11:59:02.591202Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.view(4,2).is_contiguous()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "db0f1aab",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:00:51.470906Z",
     "start_time": "2023-10-07T12:00:51.465880Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 4],\n",
       "        [1, 5],\n",
       "        [2, 6],\n",
       "        [3, 7]])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# doesn't modify the underlying memory allocation\n",
    "# 不修改内存分配\n",
    "a.t()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "853b7bec",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T11:59:51.343353Z",
     "start_time": "2023-10-07T11:59:51.334141Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFwAAACdCAYAAAA5Wx9JAAAACXBIWXMAAB6aAAAemgH8byj4AAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAE2xJREFUeJztnXl0HMWdxz/VM9LotC5bsuVDPmUb34ANNre5/AAbwhWu7ArIscCDJAssD7L7YNkXwpGEkLBZTnOF+wzBYAcwENuADQYfLPiULB+SbFmHdc9opmv/+M1YI0syZqa7Rovn816/bs30dP3mq5qq6urfr36K+BkD3KgUZ2koRpPiwDUPhSCKGjTvAL8HNvRx3pHA+cBIINWQbQD1wDLgdaAt8qKK86JXonhIQUpWCfgK4r7edyLQAM3b0NrGBn4BPBj1di7wKHAhgDedkOUzY5+20cFWlA5hKUW11lwNvAPxCX4+8ErmUP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sFf+Pwwa3vQ6Nm8SjtqVS4uyDrZJzeMwPnS4tdlp3ws5w2z7+SnPlOi74pmdE9AjKAzN+BdNuIuHes9FsfELG8INmQsE0c+U6Lvi0m2Rlh1BHOK/70+IIX/6SuDRnlThdYgxoWWgBuiLYTOG44EVzuv8941ewZIHccS2/Dub1g4cWu5ZKZfCmw9hLzJbteqdppcCsu+V4+9vuh+UdCpHOctSF5uNHjQwLc8aFD3T3TFGJILCvK1WZ6eYEDAlevy5cmFdC8xLJ5r/IJFv2KChOQCSEI4LvXS2rZPZG605YHl4FaNgZ5nKe9cXGcIjghL5m0F3GkU5zx2JYfQcUzoacsTIS6WyWNUm2vyO30OmFMPsPTpQWO/XrYO+XMlQd96PE2OCI4PlTJLy6Zpls0VipcsNzzD2JHxJ+86jsh89zd47+YDgieMkC2dqqZLjVuktqUXoRFEx3P5P2oTLrLjj6TgkXTBSOjsMzimXrryS6/4CkM6dxkoIbJim4YZKCGyYpuGGSghsmKbhhkoIbJim4YZKCGyYpuGFiFVzrfhOy1EXYJh113G8IpzvQsQpeH2zDCrY5Z1C82EHw16EIpwZr3/MtHzBM+25QHhpiFXwlWh4K9xeqlkLIjwUsUYrA9kWJtqiLYDvseg9bh/g4VsEXKYuaVbdiB5octS0mgu3w6c3YyqIReFZrntv6PNrtnBOHyuo7wN+ABTwSq+Bt2uaapi2oRadi79vkoHXfkeYKePtMdP06LG1zPdAI3IZiz+KzsaO9wEwTbIOVt8Da+wB4FfhbvI9Ry5TiYSxSBh8POaUoZfDB7L7N6JplYIcIofk58Oeotycpi7e0zcjc8diFx2KZctgEyYK1633scM1+GSgDHOn1RgEPKotyFAFMRYkpOpWHSuAhoLQP2zKAm5SHz5RFqzHbQCsPtUhm3fnRBv0ffSMozeWyYhkAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image('./imgs/shape_4x2_2.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "926758a9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:47:15.339069Z",
     "start_time": "2023-10-07T12:47:15.333869Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAT0AAAAnCAYAAAB9oZ/UAAAACXBIWXMAAB6aAAAemgH8byj4AAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAEZlJREFUeJztnXl0HNWVh79XLbX2xZIs2ZYty6u8YbxhYzYDwZCExYCdYQkJGbCHhCFDwjqBzBkghJBJWAaGcIAAw5YEwwyY2GRMCNjGG9hg4xXvtixb+95SS73Umz+umpbbklAkV1VzXN85darUVa369av3br1371ug/0wEnkdRBgQBbcumaEOxE3gIGBijKQG4AVihDJps0wRaGdQBy4EFgIrRVQQ8rgz2oWi3Ma0CyuAA8F/AiBhNCrgcWKY81NqcVk0oPgL+CUika+YAi5WiEgjbmGatKD4F7gTSYjSlAneg2ICi1cY0M5VBFfAGcG4XaTUZeFEZHEERskuXUrQpgx3Ag0BejKYEYCGKlcpjc1n0UAe8C8ynU1mMLZR/L7cD/6E8kDkSIykHjO6y7gkm3AatFeiWMhSKRjQLgPeBAhTvoJmZnIc5YCKGNwtUf39pL9AmtNdD3RbMQBMGiv9DcxXQBFylFC+h8OZNh6zRqIRU6zUBhPzQtBddvRG0JohmIfAKUphfA+Yl52EOnYuROtieZ6hN8FdB+UrM5oMYymCzNrkEONJxiQd4ArjZ48XMHI2RmAlGgvXaAEIt0HIE01+FoRSHtOZiYDswQRks0ybFaUMxs8ZgeDPt0WSGoK1G8lfIjwE8A9wChIC7gV8ZXnTB6Rjpw8CTbI+uQDM07ETXbUMpRb02mQ98CAxWHv6sw0xPH4455FyMlHxQhvWazBC0HoWy9zHbqjFQLENzNeDrjylYBDybOQpdfDkqMQPQEGyRfUKaPT+u5QjsfwMz0EgAzRwUTyuDq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9gas2+ms9Qu192WpGw6SFoq8EAnuvrNH5+oCrYzDxfGTqjGOXUGhgtZbD3NcxQK+1ovo1iWs0mBiemozJHHr8mqR2YQZmqe/+baOBdNHeFWriu4iMYOAOVlGO/JpBJXlffgtm8H4XmO0BKxWrGh9uh4AxnJpbUJnzxLHz0Q0w0W9EsAo4C6W1VnBlogIxiWYLACRq+gP2LMQlTCVyiDK6sWkda6hBU2lBnNIVaZJ2JcqmxP4rmj/5KLq3bis6fhUp0aAHvmk2w+p8JBxulLCqDWaVLyU/OQ+VNd6YshttkgfPPHgQUS4D7+ivjJ8BvlQeVUYyRnGdfwQm3QWs5uuUICkU9mgXAB0A+irfQnJGUg5kzEcM7wJ4E16b47mq3YgabMIClwLVAM7BAGbwMJOdOhawx9mXOUCs07kXXfApoAlpzI/AakAa8BMxPysUcegFGWqE9z1CbHetJrMT0lWIoDxt1mHmIwQPwAI8C/2J4MTNHYniz7TN+wRZoKcNsq8ZAsR/NpcAOYJwyWKpNRqUVYmaNxfBm2qPJDEFbNdRuwQy3YQBPAbcCYeAOFL82EiB/FkbGcPsW4wo0Qf0OdP0OlFLUapP5wEpgkPKwRIeZmTYMc8i5GKmDjl08zCrMkKyJU/ZXzPZaDGAJcB3gOxH/fzzwHIrDQBCJ2Vi/KdqUYjvwSyB2MTsPcD2KD5VBg22aQCuDWuBd4AqOD5AOBR5TBntRtNuYVgFlsB94AijieC4D/qw81NiaVh6aUKwEFtL9LN5nAa8rRQVSuO1KsxYUG4DbgJQYTSnAT1F8ohQtNqZZWBlUAq8DZ3eRVhOB55XBYRQhu3QpRZsy2Ab8Aohtx3iAG1CsUB4abUwr3ZGflwGXdxb0/xaqffwAW+5RAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<IPython.core.display.Image object>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image('./imgs/shape_1x8_3.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ad8cea42",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T11:59:39.617252Z",
     "start_time": "2023-10-07T11:59:39.608992Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 4)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 从 0 跳转到 1，1个额内存\n",
    "a.t().stride()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "17a01b5a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:04:30.337567Z",
     "start_time": "2023-10-07T12:04:30.327609Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.t().is_contiguous()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9af62d67",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:48:23.690045Z",
     "start_time": "2023-10-07T12:48:23.165017Z"
    }
   },
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[20], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43ma\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mt\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mview\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m4\u001b[39;49m\u001b[43m)\u001b[49m\n",
      "\u001b[0;31mRuntimeError\u001b[0m: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead."
     ]
    }
   ],
   "source": [
    "a.t().view(2, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "d78858e8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:05:05.285915Z",
     "start_time": "2023-10-07T12:05:05.276758Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 4, 1, 5],\n",
       "        [2, 6, 3, 7]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.t().reshape(2, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "d4b3832b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:06:23.428280Z",
     "start_time": "2023-10-07T12:06:23.418384Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 4, 1, 5],\n",
       "        [2, 6, 3, 7]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.t().contiguous().view(2, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5b89d003",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:49:21.080945Z",
     "start_time": "2023-10-07T12:49:21.073226Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 1, 1, 1],\n",
       "        [1, 1, 1, 1]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.t().contiguous().view(2, 4).fill_(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a58bfb08",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:49:22.218349Z",
     "start_time": "2023-10-07T12:49:22.209468Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1, 2, 3],\n",
       "        [4, 5, 6, 7]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "a108ac4f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:33:18.915664Z",
     "start_time": "2023-10-07T12:33:18.907909Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 1, 1, 1],\n",
       "        [1, 1, 1, 1]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.t().reshape(2, 4).fill_(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ec80d252",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-07T12:33:20.732302Z",
     "start_time": "2023-10-07T12:33:20.721683Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 1, 2, 3],\n",
       "        [4, 5, 6, 7]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03e205e9",
   "metadata": {},
   "source": [
    "### view 场景"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dd5ab24",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-09T13:44:11.336564Z",
     "start_time": "2023-10-09T13:44:11.326199Z"
    }
   },
   "source": [
    "```\n",
    "bsz, seqlen, _ = x.shape\n",
    "xq, xk, xv = self.wq(x), self.wk(x), self.wv(x)\n",
    "\n",
    "xq = xq.view(bsz, seqlen, self.n_local_heads, self.head_dim)\n",
    "xk = xk.view(bsz, seqlen, self.n_local_kv_heads, self.head_dim)\n",
    "xv = xv.view(bsz, seqlen, self.n_local_kv_heads, self.head_dim)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "915a06bb",
   "metadata": {},
   "source": [
    "## `torch.flip`/`torch.swapaxes`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "74bd283d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-08T12:15:26.788494Z",
     "start_time": "2023-09-08T12:15:26.772055Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(10)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f15f7c35",
   "metadata": {},
   "source": [
    "### flip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "37172eeb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-08T12:14:57.290356Z",
     "start_time": "2023-09-08T12:14:56.749605Z"
    }
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "step must be greater than zero",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[5], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mx\u001b[49m\u001b[43m[\u001b[49m\u001b[43m:\u001b[49m\u001b[43m:\u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m]\u001b[49m\n",
      "\u001b[0;31mValueError\u001b[0m: step must be greater than zero"
     ]
    }
   ],
   "source": [
    "x[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "f1fc5a51",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-08T12:15:28.714019Z",
     "start_time": "2023-09-08T12:15:28.703703Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([9, 8, 7, 6, 5, 4, 3, 2, 1, 0])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.flip(x, dims=(0, ))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fda0f35",
   "metadata": {},
   "source": [
    "### swapaxes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "273b4b65",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:18:15.910636Z",
     "start_time": "2023-10-14T14:18:15.901696Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0, 1],\n",
       "         [2, 3]],\n",
       "\n",
       "        [[4, 5],\n",
       "         [6, 7]]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.arange(8).reshape(2, 2, 2)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "dec1507d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:18:26.982877Z",
     "start_time": "2023-10-14T14:18:26.975428Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0, 4],\n",
       "         [2, 6]],\n",
       "\n",
       "        [[1, 5],\n",
       "         [3, 7]]])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.swapaxes(x, 0, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "45601773",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:19:02.864632Z",
     "start_time": "2023-10-14T14:19:02.847459Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0, 4],\n",
       "         [2, 6]],\n",
       "\n",
       "        [[1, 5],\n",
       "         [3, 7]]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.einsum('ijk->kji', x)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b508e81f",
   "metadata": {},
   "source": [
    "## `torch.flatten`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7fe3ee29",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-09T14:31:43.700681Z",
     "start_time": "2023-10-09T14:31:43.693023Z"
    }
   },
   "outputs": [],
   "source": [
    "t = torch.tensor([[[1, 2],\n",
    "                   [3, 4]],\n",
    "                  [[5, 6],\n",
    "                   [7, 8]]])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6a202a69",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-09T14:31:54.765660Z",
     "start_time": "2023-10-09T14:31:54.758022Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 2, 3, 4, 5, 6, 7, 8])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.flatten(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "cdaa7140",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-09T14:32:04.916382Z",
     "start_time": "2023-10-09T14:32:04.908902Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 2, 3, 4],\n",
       "        [5, 6, 7, 8]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.flatten(t, start_dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "ad1da7d3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-09T14:32:10.395477Z",
     "start_time": "2023-10-09T14:32:10.386172Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1, 2],\n",
       "         [3, 4]],\n",
       "\n",
       "        [[5, 6],\n",
       "         [7, 8]]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "dd3f7e7a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-09T14:32:41.582100Z",
     "start_time": "2023-10-09T14:32:41.575862Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 2, 3, 4],\n",
       "        [5, 6, 7, 8]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# flatten(start_dim=0, end_dim=-1) \n",
    "t.flatten(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "514e1fe6",
   "metadata": {},
   "source": [
    "## expand"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f2a670c",
   "metadata": {},
   "source": [
    "- expand只能扩展维度为1的维度；"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "e6538d9b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:22:15.793534Z",
     "start_time": "2023-10-14T14:22:15.788331Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 1])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.tensor([[1], \n",
    "                  [2], \n",
    "                  [3]])\n",
    "x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "ac3f078e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:22:26.693832Z",
     "start_time": "2023-10-14T14:22:26.681232Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 1, 1, 1],\n",
       "        [2, 2, 2, 2],\n",
       "        [3, 3, 3, 3]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.expand(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "9d2a7fc5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:22:34.637949Z",
     "start_time": "2023-10-14T14:22:34.631355Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1, 1, 1, 1],\n",
       "        [2, 2, 2, 2],\n",
       "        [3, 3, 3, 3]])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.expand(-1, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "c3754a1d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:24:37.644795Z",
     "start_time": "2023-10-14T14:24:37.636104Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4],\n",
       "        [2],\n",
       "        [3]])"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.expand(3, 4)[0][0] = 4\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "128b9c4d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:24:49.158699Z",
     "start_time": "2023-10-14T14:24:49.148710Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4],\n",
       "        [2],\n",
       "        [3]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c5bbee1d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:25:03.749416Z",
     "start_time": "2023-10-14T14:25:03.739299Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4, 4, 4, 4],\n",
       "        [2, 2, 2, 2],\n",
       "        [3, 3, 3, 3]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.expand(-1, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "d702a59e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T14:25:14.728008Z",
     "start_time": "2023-10-14T14:25:14.721089Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[4],\n",
       "        [5],\n",
       "        [3]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.expand(-1, 4)[1, 1] = 5\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4383273c",
   "metadata": {},
   "source": [
    "## `torch.transpose()`/`torch.permute()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48b109bd",
   "metadata": {},
   "source": [
    "- 两者只是接口（或者入参）不同？？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "8613eeba",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T15:49:58.895703Z",
     "start_time": "2023-10-14T15:49:58.888916Z"
    }
   },
   "outputs": [],
   "source": [
    "x = torch.randint(0, 5, (2, 3, 4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "a87dcaab",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T15:50:00.666774Z",
     "start_time": "2023-10-14T15:50:00.658974Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[4, 4, 1, 1],\n",
       "         [1, 0, 4, 2],\n",
       "         [2, 4, 2, 0]],\n",
       "\n",
       "        [[2, 2, 2, 0],\n",
       "         [3, 3, 3, 4],\n",
       "         [1, 4, 2, 1]]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "d275b138",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T15:50:02.690393Z",
     "start_time": "2023-10-14T15:50:02.679115Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.equal(x.transpose(1, 2), x.transpose(2, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "b3d22b54",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T15:50:06.622255Z",
     "start_time": "2023-10-14T15:50:06.612563Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.equal(x.transpose(1, 2), x.permute((0, 2, 1)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "192d3c44",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-10-14T15:50:25.573494Z",
     "start_time": "2023-10-14T15:50:25.562363Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.equal(torch.swapaxes(x, 1, 2), x.transpose(1, 2))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
